var DaterangeControl = Class.create({
    input:null,
    view:null,
    fromdate:null,
    todate:null,
    quickselect: null,
    drop:null,

    initialize: function(element)
    {
        this.input = $(element);
        this.input.daterange = this;
        this.view = Builder.node('div', {className: 'daterange'});
        this.view.update(
            'from <input class="datepicker fromdate"> to <input class="datepicker todate">'+
            '<div>or'+
                '<select class="quickselect">'+
                    '<option value="null">Quick select</option>'+
                    '<option value="0">Today</option>'+
                    '<option value="-1">Past 1 day</option>'+
                    '<option value="-2">Past 2 days</option>'+
                    '<option value="-3">Past 3 days</option>'+
                    '<option value="-7">Past 7 days</option>'+
                    '<option value="-30">Past 30 days</option>'+
                    '<option value="-60">Past 60 days</option>'+
                    '<option value="-90">Past 90 days</option>'+
                '</select>'+
            '</div>'+
            '<div class="action"><span class="action_item apply">Apply</span> <span class="action_item cancel">Cancel</span></div>'
        );
        this.drop = new DropdownControl(this.input, this.view);
        this.observeEvent();
    },

    observeEvent: function()
    {
        var me = this;
        this.fromdate = this.view.down('.fromdate');
        this.todate = this.view.down('.todate');
        this.quickselect = this.view.down('.quickselect');

        this.view.find('.datepicker').invoke('observe', 'click',function(){
            new CalendarDateSelect(this);
        });

        this.view.down('.action_item.apply').observe('click', function(){
            me.apply();
        });
        this.view.down('.action_item.cancel').observe('click', function(){
			me.drop.hide();
        });

        this.quickselect.observe('change', function(){
            me.setQuickSelect(this.value);
        });
    },

    setQuickSelect: function(offset)
    {
        if (offset == 'null') return;
        offset = parseInt(offset);
        var today = new Date();
        this.todate.value = this.formatOffsetDate(today, 1);
        this.fromdate.value = this.formatOffsetDate(today, offset);
    },

    offsetDay: function(date, offset)
    {
        var newDate = new Date();
        newDate.setDate(date.getDate()+offset);
        return newDate;
    },

    formatOffsetDate: function(date, offset)
    {
        var offsetDate  = this.offsetDay(date, offset);
        return this.formatDate(offsetDate);
    },

    apply: function()
    {
        this.input.value = this.getRangeValue();
        this.drop.hide();
        if (this.applyCallback)
            this.applyCallback();
    },

    getRangeValue: function()
    {
        var fromdate = this.fromdate.value;
        var todate = this.todate.value;
        if (todate && !fromdate)
            return "<= '" + todate + "'";
        else if (fromdate && !todate)
            return ">= '" + fromdate + "'";
        else if (fromdate && todate)
            return "BETWEEN '" + fromdate + "' AND '" + todate + "'";
        else
            return null;
    },

    formatDate: function(d)
    {
        var month = d.getMonth()+1;
        if (month<10) month = '0'+month;
        return d.getFullYear()+'-'+month+'-'+d.getDate();
    }
});